package com.jvtd.eyl.widget;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.util.AttributeSet;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jvtd.eyl.R;
import com.jvtd.eyl.bean.binding.BookBean;
import com.jvtd.eyl.bean.http.MyBookBean;
import com.jvtd.eyl.constants.App;
import com.jvtd.eyl.utils.ReadUtils;
import com.jvtd.eyl.widget.adapter.MyBookAdapter;
import com.jvtd.widget.emptyView.JvtdEmptyView;
import com.jvtd.widget.recyclerView.JvtdRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的书架列表
 * 作者:chenlei
 * 时间:2018/11/15 1:27 PM
 */
public class MyBookRecyclerView extends JvtdRecyclerView<MyBookBean> {
    private MyBookAdapter mAdapter;

    private OnSelectedChangeListener mOnSelectedChangeListener;

    private boolean isEditor = false;

    public MyBookRecyclerView(@NonNull Context context) {
        super(context);
    }

    public MyBookRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyBookRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public BaseQuickAdapter initAdapter() {
        setBackgroundColor(ContextCompat.getColor(mContext, R.color.color_99fdf2e3));
        mAdapter = new MyBookAdapter();
//        mAdapter.openLoadAnimation();

        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            MyBookBean bookBean = mAdapter.getItem(position);
            if (!isEditor) {
                if (view.getId() == R.id.show_btn) {
                    if (bookBean != null && !bookBean.isEmpty()) {
                        List<BookBean> bookBeans = new ArrayList<>();
                        for (MyBookBean myBookBean : mAdapter.getData()) {
                            if (!myBookBean.isEmpty())
                                bookBeans.add(myBookBean.getDocumentperiodical());
                        }
                        ReadUtils.readFile(mContext,bookBeans,position);
                    }
                }
            }else {
                if (view.getId() == R.id.check_box){
                    if (mOnSelectedChangeListener != null)
                        mOnSelectedChangeListener.onSelectChange();
                }
            }
        });
        return mAdapter;
    }

    @Override
    public LayoutManager initLayoutManager() {
        return new GridLayoutManager(mContext,3);
    }

    @Override
    public boolean isFirst() {
        return false;
    }

    @Override
    public int getLoadNum() {
        return 0;
    }

    /**
     * 重写设置数据方法 确保数量为3的倍数
     * @param data 数据
     * @return 当前控件
     */
    @Override
    public JvtdRecyclerView setData(List<MyBookBean> data) {
        List<MyBookBean> myBookBeans = new ArrayList<>(data);
        int addNum = 3 - myBookBeans.size() % 3;
        if (addNum == 3) addNum = 0;
        for (int i = 0; i < addNum; i++) {
            MyBookBean myBookBean = new MyBookBean();
            myBookBean.setEmpty(true);
            myBookBeans.add(myBookBean);
        }
        return super.setData(myBookBeans);
    }

    @Override
    protected void configEmptyView(JvtdEmptyView emptyView) {
        emptyView.setEmptyImage(0)
                .setErrorText(R.string.loading_failed_touch)
                .setErrorImage(0)
                .setLoadingText("")
                .setEmptyText(R.string.empty_browse_tips);
    }

    /**
     * 是否编辑模式
     * @param editor 是与否
     */
    public void editor(boolean editor) {
        isEditor = editor;
        if (!editor) {
            for (MyBookBean myBookBean : mAdapter.getData()) {
                myBookBean.getDocumentperiodical().setSelected(false);
            }
        }
        mAdapter.setSelected(editor);
        mAdapter.notifyDataSetChanged();
    }

    public boolean isEditor() {
        return isEditor;
    }

    /**
     * 全选
     * @param all 是与否
     */
    public void selectAll(boolean all){
        if (isEditor){
            for (MyBookBean myBookBean : mAdapter.getData()) {
                myBookBean.getDocumentperiodical().setSelected(all);
            }
        }
        if (mOnSelectedChangeListener != null)
            mOnSelectedChangeListener.onSelectChange();
    }

    public void setOnSelectedChangeListener(OnSelectedChangeListener onSelectedChangeListener) {
        mOnSelectedChangeListener = onSelectedChangeListener;
    }

    //获取选择数据
    public List<MyBookBean> getSelectedData() {
        List<MyBookBean> myBookBeans = new ArrayList<>();
        for (MyBookBean myBookBean : mAdapter.getData()) {
            if (myBookBean.getDocumentperiodical().isSelected())
                myBookBeans.add(myBookBean);
        }
        return myBookBeans;
    }

    @Override
    public double getFlingRatio()
    {
        return App.FLING_RATIO;
    }
}
